package com.lxu.report.util;

import com.lxu.report.entity.WordEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.xwpf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * poi doc 工具类
 */
public class XWPFUtils {

    private static Logger logger = LoggerFactory.getLogger(XWPFUtils.class);

    /**
     * 获取doc中所有table
     *
     * @param in
     * @return
     * @throws IOException
     */
    public static List<XWPFTable> getAllTables(InputStream in) throws IOException {
        XWPFDocument xwpfDocument = new XWPFDocument(in);
        return xwpfDocument.getTables();
    }

    /**
     * 获取指定下标的表格
     *
     * @param xwpfDocument
     * @param index
     * @return
     * @throws IOException
     */
    public static XWPFTable getTableByIndex(XWPFDocument xwpfDocument, int index) throws IOException {
        List<XWPFTable> tables = xwpfDocument.getTables();
        if (CollectionUtils.isEmpty(tables)) {
            logger.info("文档中无表格");
            return null;
        }
        if (index < 0 || index > tables.size() - 1) {
            logger.error("下标错误，请确认");
            return null;
        }
        return tables.get(index);
    }

    /**
     * 从表格的指定行开始，插入指定数量的行
     *
     * @param table
     * @param index
     * @param rows
     */
    public static void insertRows(XWPFTable table, int index, int rows) {
        XWPFTableRow tableRow = table.getRow(index);
        if (tableRow != null) {
            for (int i = 0; i < rows; i++) {
                table.addRow(tableRow, index);
            }
        }
    }

    /**
     * 从表格的指定行开始，插入数据
     *
     * @param table
     * @param index
     * @param rowDatas
     */
    public static void insertRowDatas(XWPFTable table, int index, List<List<String>> rowDatas) {
        List<XWPFTableRow> rows = table.getRows();
        for (int i = index; i <= rowDatas.size(); i++) {
            XWPFTableRow newRow = table.getRow(i);
            List<XWPFTableCell> cells = newRow.getTableCells();
            for (int j = 0; j < cells.size(); j++) {
                XWPFTableCell cell = cells.get(j);
                XWPFParagraph xwpfParagraph = cell.getParagraphs().get(0);//设置字体样式和内容
                XWPFRun run = xwpfParagraph.createRun();
                run.setFontSize(9);
                run.setFontFamily("宋体");
                run.setText(rowDatas.get(i - 1).get(j));
            }
        }
    }

    /**
     * 填充复杂模板，既包含段落 又包含表格
     *
     * @param in
     * @param wordEntity
     * @return
     */
    public static XWPFDocument fillComplexWord(InputStream in, WordEntity wordEntity) {
        XWPFDocument document = null;
        try {
            //第一步：获取docx解析对象
            document = new XWPFDocument(in);
            //第二步：重构XWPFDocument中 不合格的 占位符
            XWPFDocumentRefactorUtils.refactorXWPFDocument(document);
            //第三步：解析替换文本段落对象
            XWPFDocumentTextUtils.changeText(document, wordEntity);
            //第四步：解析替换表格对象
            XWPFDocumentTableUtils.changeTableProvince(document, wordEntity);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return document;
    }

    /**
     * 保存 word
     *
     * @param file
     * @param xwpfDocument
     * @throws Exception
     */
    public static void save(String file, XWPFDocument xwpfDocument) throws Exception {
        FileOutputStream out = new FileOutputStream(file);
        xwpfDocument.write(out);
        out.flush();
        out.close();
    }


}
